<?php

	session_start();
	//生成一张黑底100*30px的底图
	$image = imagecreatetruecolor(100, 30);
	//颜色设置为白色
	$bgcolor = imagecolorallocate($image, 255, 255, 255);
	//从左上角填充$bgcolor
	imagefill($image, 0, 0, $bgcolor);
	
	//生成随机数字
/*	for($i = 0; $i < 4; $i++) {
		$fontsize = 6;
		$fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
		$fontcontent = rand(0, 9);
		$x = ($i*100/4) + rand(5, 10);
		$y = rand(5, 10);
		
		imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
	}*/
	
	$captcha_code = '';
	//随机验证码，字符数字组合
	for($i = 0; $i < 4; $i++) {
		$fontsize = 7;
		$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
		$data = "abcdefghijklmnopqrstuvwxyz0123456789";
		$fontcontent = substr($data, rand(0, strlen($data)-1), 1);
		$captcha_code .= $fontcontent;
		$x = ($i*100/4) + rand(5, 10);
		$y = rand(5, 10);
		
		imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
	}
	$_SESSION['authcode'] = $captcha_code;
	
	//干扰点
	for($i = 0; $i < 200; $i++) {
		$pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
		imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor);
	}
	
	//干扰线
	for($i = 0; $i < 4; $i++) {
		$linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));
		$x= rand(1, 99);
		$y = rand(1, 29);
		imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1, 29), $linecolor);
	}
	
	header("Content-Type:image/png");
	imagepng($image);
	
	//destroy
	imagedestroy($image)
?>